package com.samehope.ar.service.impl;

import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.dto.ItemTypeAddParam;
import com.samehope.ar.dto.ItemTypeUpdateParam;
import com.samehope.ar.entity.PatrolItem;
import com.samehope.ar.entity.PatrolItemType;
import com.samehope.ar.mapper.PatrolItemTypeMapper;
import com.samehope.ar.service.IPatrolItemService;
import com.samehope.ar.service.IPatrolItemTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.util.TreeUtils;
import com.samehope.ar.vo.FormSelectVO;
import com.samehope.ar.vo.TreeDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 巡检项分类表 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@Service
public class PatrolItemTypeServiceImpl extends ServiceImpl<PatrolItemTypeMapper, PatrolItemType> implements IPatrolItemTypeService {

    @Autowired
    private IPatrolItemService itemService;

    /**
     * 巡检项分类增加
     * @param param
     */
    @Override
    public Long add(ItemTypeAddParam param) {

        PatrolItemType itemType = this.baseMapper
                .findByProjectAndItemTypeName(SessionUtil.getProjectId(), param.getName());

        if (itemType != null) {
            throw new IllegalArgumentException("分类名称已存在");
        }

        itemType = new PatrolItemType();
        itemType.setCompanyId(SessionUtil.getCompanyId());
        itemType.setProjectId(SessionUtil.getProjectId());
        itemType.setName(param.getName());
        itemType.setCreateBy(SessionUtil.getUserId());
        this.save(itemType);

        return itemType.getId();
    }

    /**
     * 巡检项分类删除
     * @param itemTypeId
     */
    @Override
    public void delete(Long itemTypeId) {
        PatrolItemType itemType = this.baseMapper
                .findByProjectAndItemType(SessionUtil.getProjectId(), itemTypeId);

        if (itemType == null) {
            throw new IllegalArgumentException("分类不存在");
        }

        List<PatrolItem> items = itemService.findByItemType(itemTypeId);
        if (items.size() > 0) {
            throw new IllegalArgumentException("分类下存在巡检项, 无法删除");
        }

        itemType.setDelFlag(DelFlagEnum.DELETED.getCode());
        itemType.setUpdateBy(SessionUtil.getUserId());
        itemType.setUpdateDate(LocalDateTime.now());
        this.updateById(itemType);
    }

    /**
     * 更新巡检项分类
     * @param param
     */
    @Override
    public void update(ItemTypeUpdateParam param) {
        PatrolItemType itemType = this.baseMapper
                .findByProjectAndItemType(SessionUtil.getProjectId(), param.getItemTypeId());

        if (itemType == null) {
            throw new IllegalArgumentException("分类不存在");
        }

        itemType.setName(param.getItemTypeName());
        itemType.setSortNo(param.getSortNo());
        itemType.setUpdateBy(SessionUtil.getUserId());
        itemType.setUpdateDate(LocalDateTime.now());
        this.updateById(itemType);
    }

    /**
     * 巡检项分类下拉选视图对象集合
     * @return
     */
    @Override
    public List<FormSelectVO> typesForSelect() {
        return this.baseMapper.typesForSelectByProject(SessionUtil.getProjectId());
    }

    /**
     * 巡检项分类树结构
     * @param condition
     * @return
     */
    @Override
    public List<TreeDataVO> tree(String condition) {
        List<TreeDataVO> tree = this.baseMapper.findTreeByProjectAndCondition(SessionUtil.getProjectId(), condition);
        TreeUtils.setLevel(tree, 1);
        TreeUtils.setId(tree);
        return tree;
    }

    /**
     * 根据巡检项分类名称查询巡检项
     * @param itemTypeName
     * @return
     */
    @Override
    public PatrolItemType findTypeByTypeName(String itemTypeName) {
        return this.baseMapper.findByProjectAndItemTypeName(SessionUtil.getProjectId(), itemTypeName);
    }
}
